package com.server.interceptor;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.server.authority.Login;
import com.server.authority.ResultTypeEnum;
import com.server.common.base.SessionData;
import com.server.web.SessionUtil;




public class LoginAnnotationInterceptor extends HandlerInterceptorAdapter{
	
	final Logger logger = LoggerFactory.getLogger(getClass());
	

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HandlerMethod handler2 = (HandlerMethod) handler;
		Login login = handler2.getMethodAnnotation(Login.class);
		if (null == login) {
			// 没有声明权限,放行
			return true;
		}
		String user = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		if(StringUtils.isEmpty(user)){
			// 需要登录
			if (login.value() == ResultTypeEnum.page) {
				//采用传统页面进行登录提示
				request.getRequestDispatcher("/login").forward(request, response);
			} else if (login.value() == ResultTypeEnum.json) {
				//采用ajax方式的进行登录提示
				response.setCharacterEncoding("utf-8");
				response.setContentType("text/html;charset=UTF-8");
				OutputStream out = response.getOutputStream();
				PrintWriter pw = new PrintWriter(new OutputStreamWriter(out,"utf-8"));
				pw.println("{\"result\":false,\"code\":11,\"errorMessage\":\"您未登录,请先登录!\"}");
				pw.flush();
				pw.close();
			}
			return false;
		}
		return true;
	}

}
